﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System.Collections.Generic;
using System.ComponentModel;

/// <summary>
/// 简单帐表：利润分析表插件
/// </summary>
/// <remarks>
/// 功能：
/// 创建人：刘雪丽
/// 创建日期：2024-6-6
/// 修改信息（1）：修改人，修改日期，修改内容
/// </remarks>
namespace LM.CB.K3Cloud.Plugin
{
    [Description("利润分析表插件"), HotUpdate]
    public class ProfitAnalyRptPlugin : SysReportBaseService
    {
        public override void Initialize()
        {

            // 简单账表类型：基本简单账表
            ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            //标识报表是否调用BuilderReportSqlAndTempTable接口创建临时表
            IsCreateTempTableByPlugin = true;
            //不在ui设计列
            this.ReportProperty.IsUIDesignerColumns = false;
            ReportProperty.ReportName = new Kingdee.BOS.LocaleValue("利润分析表");
            // ReportProperty.IdentityFieldName = "FIDENTITYID";
            // 标识报表支持汇总
            this.ReportProperty.IsGroupSummary = true;
            //合并字段主键
            //this.ReportProperty.PrimaryKeyFieldName = "FPRODUCTID_FNUMBER";
            //将产成品的id替换成名称显示
            // this.ReportProperty.DspInsteadColumnsInfo.DefaultDspInsteadColumns.Add("faterMaterial", "faterMaterialName");
        }

        /// <summary>
        /// 设置报表头
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            ReportTitles titles = new ReportTitles();
            DynamicObject myfilter = filter.FilterParameter.CustomFilter;
            if (myfilter != null)
            {
                //组织
                DynamicObject F_QLTK_ACCTGORGObj = myfilter["F_QLTK_ACCTGORGID"] as DynamicObject;
                string F_QLTK_ACCTGORGID = F_QLTK_ACCTGORGObj["Name"]?.ToString();
                //开始日期
                string F_QLTK_STARTDATE = myfilter["F_QLTK_STARTDATE"].ToString();
                //结束日期
                string F_QLTK_ENDDATE = myfilter["F_QLTK_ENDDATE"].ToString();
                titles.AddTitle("F_QLTK_ACCTGORGID", F_QLTK_ACCTGORGID);
                titles.AddTitle("F_QLTK_STARTDATE", F_QLTK_STARTDATE);
                titles.AddTitle("F_QLTK_ENDDATE", F_QLTK_ENDDATE);
            }
            return titles;
        }

        /// <summary>
        /// 构造取数Sql，取数据填充到临时表：tableName
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="tableName"></param>
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            base.BuilderReportSqlAndTempTable(filter, tableName);
            //默认排序字段：需要从filter中取用户设置的排序字段
            //KSQL_SEQ: ROW_NUMBER() OVER(ORDER BY  {0} ) FIDENTITYID
            string sortStr = filter.FilterParameter.SortString;
            KSQL_SEQ = string.Format(KSQL_SEQ, sortStr.IsNullOrEmptyOrWhiteSpace() ? " FDATE " : sortStr);
            string sql;
            //1、解析过滤框
            DynamicObject myfilter = filter.FilterParameter.CustomFilter;
            //开始日期
            string F_QLTK_STARTDATE = myfilter["F_QLTK_STARTDATE"].ToString();
            //结束日期
            string F_QLTK_ENDDATE = myfilter["F_QLTK_ENDDATE"].ToString();
            //组织
            string F_QLTK_ACCTGORGID = myfilter["F_QLTK_ACCTGORGID_id"].ToString();
            //2、执行查询操作
            sql =
            $@"EXEC [dbo].[dev_p_ProfitRpt] @tempFilterTable = N'{tableName}',
		    @KSQL_SEQ = N'{KSQL_SEQ}',@fstartDate='{F_QLTK_STARTDATE}',@fendDate='{F_QLTK_ENDDATE}',@forgid={F_QLTK_ACCTGORGID}";
            DBUtils.Execute(Context, sql);
        }
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("F_QLTK_RevAmt", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("F_QLTK_CostAmt", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("F_QLTK_StandCostAmt", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("F_QLTK_ProfitAmt", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("F_QLTK_StandProfitAmt", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("F_QLTK_RevAmtNoTax", Kingdee.BOS.Core.Enums.BOSEnums.Enu_SummaryType.SUM));
            return result;//base.GetSummaryColumnInfo(filter);
        }
    }


}
